package com.wuwei.business.controller;

import com.wuwei.business.domain.BusCustomer;
import com.wuwei.business.service.IBusCustomerService;
import com.wuwei.common.core.controller.BaseController;
import com.wuwei.common.core.domain.AjaxResult;
import com.wuwei.common.core.domain.model.LoginUser;
import com.wuwei.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 内容: SSM
 * 作者: weishao
 */
@RestController
@Api(tags = "客户管理接口")
@RequestMapping("business/customer")
public class BusCustomerController extends BaseController {

    @Autowired
    private IBusCustomerService customerService;

    /**
     * 分页查询
     * TableDataInfo是一个分页类
     * startPage开启分页
     * @param busCustomer
     * @return
     */
    @GetMapping("list")
    @ApiOperation("查询用户")
    public TableDataInfo getCustomerPage(BusCustomer busCustomer){
        //分页
        startPage();
        LoginUser loginUser = getLoginUser();
        List<BusCustomer> busCustomers = customerService.selectCustomerPage(loginUser, busCustomer);
        return getDataTable(busCustomers);
    }

    /**
     *后端怎么变成JSON返回前端
     * HttpMessageConvert
     * requestBody前台穿JSON
     * @param busCustomer
     * @return
     */
    @PostMapping
    @ApiOperation("添加用户")
    public AjaxResult addCustomer(@RequestBody BusCustomer busCustomer){
        //获取当前用户
        LoginUser loginUser = getLoginUser();
        //查询部门
        int i = customerService.insertCustomer(busCustomer,loginUser);
        return toAjax(i);
    }

    /**
     * 回显通过id查用rest风格PathVariable
     * @param id
     * @return
     */
    @GetMapping("{id}")
    @ApiOperation("回显数据")
    public AjaxResult getCustomerInfo(@PathVariable("id") Long id)
    {
        BusCustomer busCustomer = customerService.selectBusCustomerById(id);
        return AjaxResult.success(busCustomer);
    }
    /**
     *  修改
     * @param busCustomer
     * @return
     */
    @PutMapping
    @ApiOperation("修改用户")
    public AjaxResult updateCustomer(@RequestBody BusCustomer busCustomer){
        LoginUser loginUser = getLoginUser();
        int i=customerService.updateCustomer(loginUser,busCustomer);
        return toAjax(i);
    }

    /**
     * 根据id删除车辆信息
     * @param ids
     * @return
     */
    @DeleteMapping("{ids}")
    @ApiOperation("根据id删除")
    public AjaxResult deleteCustomer(@PathVariable("ids") List<Long> ids)
    {
        LoginUser loginUser = getLoginUser();
        int i=customerService.delCustomer(loginUser,ids);
        return toAjax(i);
    }

}
